package com.rundatop.wx.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class AliyunSmsUtil {
    
    // 产品名称
    private static final String PRODUCT = "Dysmsapi";
    // 产品域名
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
    
    // 替换成你的AccessKey ID和Secret
    private static final String ACCESS_KEY_ID = "LTAI5tNVpsFf8H8ycvehm6gr";
    private static final String ACCESS_KEY_SECRET = "dyROPnjSRvTyHXPZHCmZoIg5rl4oii";
    
    /**
     * 发送短信
     * @param phoneNumbers 手机号，多个号码用逗号分隔
     * @param signName 短信签名
     * @param templateCode 短信模板ID
     * @param templateParam 模板参数JSON字符串
     * @return 发送结果
     */
    public static SendSmsResponse sendSms(String phoneNumbers, String signName, 
                                         String templateCode, String templateParam) {
        try {
            // 初始化acsClient
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            
            // 组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            // 必填:待发送手机号
            request.setPhoneNumbers(phoneNumbers);
            // 必填:短信签名
            request.setSignName(signName);
            // 必填:短信模板Code
            request.setTemplateCode(templateCode);
            // 可选:模板中的变量替换JSON串
            if(templateParam != null) {
                request.setTemplateParam(templateParam);
            }
            
            // 发送短信
            SendSmsResponse response = acsClient.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    // 使用示例
    public static void main(String[] args) {
        String phoneNumbers = "15165611976";
        String signName = "手机虾";
        String templateCode = "SMS_489785208";
        String templateParam = "{\"code\":\"1234\"}";
        
        SendSmsResponse response = sendSms(phoneNumbers, signName, templateCode, templateParam);
        if(response != null && "OK".equals(response.getCode())) {
            System.out.println("短信发送成功!");
        } else {
            System.out.println("短信发送失败!");
        }
    }
}